package zengjin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import zengjin.common.PageResponse;
import zengjin.common.R;
import zengjin.model.dto.resume.AddResumeRequest;
import zengjin.model.dto.resume.PageResumeRequest;
import zengjin.model.dto.resume.UpdateResumeRequest;
import zengjin.model.en.Image;
import zengjin.model.en.Resume;
import zengjin.model.vo.resume.ResumeVO;
import zengjin.service.ImageService;
import zengjin.service.ResumeService;

/**
 * @author 19324
 */
@Tag(name = "简历相关接口")
@RestController
@RequestMapping("/resume")
@Slf4j
public class ResumeController {

    @Resource
    private ResumeService resumeService;

    @Resource
    private ImageService imageService;

    //1.添加简历
    @Operation(summary = "添加简历")
    @PostMapping("/addResume")
    public R<Boolean> addResume(@RequestBody AddResumeRequest addResumeRequest) {
        //1.参数校验
        if (addResumeRequest == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        Boolean addResumeResult = resumeService.addResume(addResumeRequest);
        if (!addResumeResult) {
            return R.error(false, "添加简历失败");
        }
        return R.success(true, "添加简历成功");
    }

    //2.查询简历详情根据JobseekerId
    @Operation(summary = "查询简历详情根据JobseekerId")
    @PostMapping("/getResumeDetailByJobseekerId")
    public R<ResumeVO> getResumeDetailByJobseekerId(Long resumeJobseekerId) {
        //1.参数校验
        if (resumeJobseekerId == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        ResumeVO resumeVO = resumeService.getResumeDetailByJobseekerId(resumeJobseekerId);
        if (resumeVO == null) {
            return R.error(null, "您还未添加简历");
        }
        return R.success(resumeVO, "查询简历详情成功");

    }

    //3.修改简历根据Id
    @Operation(summary = "修改简历根据Id")
    @PostMapping("/updateResumeById")
    public R<Boolean> updateResumeById(@RequestBody UpdateResumeRequest updateResumeRequest) {
        //1.参数校验
        if (updateResumeRequest == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        Boolean updateResumeResult = resumeService.updateResumeById(updateResumeRequest);
        if (!updateResumeResult) {
            return R.error(false, "修改简历失败");
        }
        return R.success(true, "修改简历成功");
    }

    //4.删除简历根据Id
    @Operation(summary = "删除简历根据Id")
    @PostMapping("/deleteResumeById")
    public R<Boolean> deleteResumeById(Long resumeId) {
        //1.参数校验
        if (resumeId == null) {
            throw new RuntimeException("参数为空");
        }
        return R.success(resumeService.removeById(resumeId), "删除简历成功");
    }

    //5.管理员-修改荣誉认证状态
    @Operation(summary = "管理员-修改荣誉认证状态")
    @PostMapping("/updateResumeVerified")
    public R<Boolean> updateResumeVerified(Long resumeId, String resumeCertificatesVerified) {
        //1.参数校验
        if (resumeId == null || resumeCertificatesVerified == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        boolean result = resumeService.updateResumeVerified(resumeId, resumeCertificatesVerified);
        if (!result) {
            return R.error(false, "修改荣誉认证状态失败");
        }
        return R.success(true, "修改荣誉认证状态成功");
    }

    //6.分页获取简历
    @Operation(summary = "分页获取简历")
    @PostMapping("/pageResume")
    public R<PageResponse<ResumeVO>> pageResume(@RequestBody PageResumeRequest pageResumeRequest) {
        //1.参数校验
        if (pageResumeRequest == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        PageResponse<ResumeVO> pageResumeResult = resumeService.pageResume(pageResumeRequest);
        if (pageResumeResult == null) {
            return R.error(null,"分页获取简历失败");
        }
        return R.success(pageResumeResult,"分页获取简历成功");


    }

    //7.查询简历详情根据简历id
    @Operation(summary = "查询简历详情根据简历id")
    @PostMapping("/getResumeDetailByResumeId")
    public R<ResumeVO> getResumeDetailByResumeId(Long resumeId) {
        //1.参数校验
        if (resumeId == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        Resume byId = resumeService.getById(resumeId);
        //查询出简历照片
        QueryWrapper<Image> imageQueryWrapper = new QueryWrapper<>();
        imageQueryWrapper.eq("image_name", "resume_avatar");
        imageQueryWrapper.eq("image_name_id", byId.getResumeId());
        Image image = imageService.getOne(imageQueryWrapper);
        ResumeVO resumeVO = BeanUtil.copyProperties(byId, ResumeVO.class);
        resumeVO.setResumeAvatar(image.getImageUrl());
        return R.success(resumeVO, "查询简历详情成功");
    }


}
